<?php
/**
 * 职位数据模型
 */

class PostModel extends RelationModel {
	protected $_link = array(
		'department' => array(
			'mapping_type' => BELONGS_TO,
			'class_name' => 'department',
		),
		'members' => array(
			'mapping_type' => HAS_MANY,
			'class_name' => 'member',
		),
	);

    protected function _after_select(&$results, $options) {
        parent::_after_select($results, $options);
        foreach ($results as &$result)
            $this->pullRelations($result);
    }

    protected function _after_find(&$result, $options) {
        parent::_after_find($result, $options);
        $this->pullRelations($result);
    }

    protected function pullRelations(&$result) {
        foreach ($result['members'] as &$member) {
            $member['post'] = M('Post')->where(array(
                'id' => $member['post_id']))->find();
            $member['department'] = D('department')->where(array(
                'id' => $member['post']['department_id']))->find();
            $member['session'] = D('session')->where(array(
                'id' => $member['department']['session_id']))->find();
        }
    }
}
